﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDataBaseWork;
using UEntity.LogEntity;

namespace AD.Manage
{
    class Program
    {
        static void Main(string[] args)
        {
            string strSqlConn = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SQLConnString"]));//

            string SyncType = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SyncType"]));//
            string DcPath1 = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["DcPath1"]));//
            string DcPath2 = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["DcPath2"]));//
            CBaseDB m_db = m_db = new CBaseDB(strSqlConn);
            string strError = string.Empty;
            if (SyncType == "1")
            {
                try
                {
                    ADSync adsync1 = new ADSync();
                    var res1 =adsync1.ADSyncManage(DcPath1);
                    ADSync adsync2 = new ADSync();
                    var res2 = adsync2.ADSyncManage(DcPath2);
                    if(res1.Result.Item1&& res2.Result.Item1)
                    {
                        if (!m_db.ExcuteBySql(" truncate table dbo.AD_OU_Temp ", out  strError))
                        {
                            ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************清空OU信息失败, error:" + strError));
                          
                        }
                        if (!m_db.ExcuteBySql(" truncate table dbo.AD_User_Temp ", out strError))
                        {
                            ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************清空User信息失败, error:" + strError));
                           
                        }
                        ADSync.InsertToDataBase(strSqlConn, res1.Result.Item2, res1.Result.Item3);
                        ADSync.InsertToDataBase(strSqlConn, res2.Result.Item2, res2.Result.Item3);

                    }
                    
                   
                    
                    CParameters parametersOU = new CParameters();
                    if (!m_db.ExcuteByTransaction(parametersOU, "spTranscationOU", out  strError))
                    {
                        ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************导入OU信息失败, error:" + strError));
                       
                    }
                    ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************导入OU信息成功:"));
                    CParameters parametersUser = new CParameters();
                    if (!m_db.ExcuteByTransaction(parametersUser, "spTranscationUserInfo", out  strError))
                    {
                        ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************导入User信息失败, error:" + strError));
                        
                    }
                    ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************导入User信息成功"));
                }
                catch (Exception)
                {

                    throw;
                }

            }
            else if (SyncType == "2")
            {
                User_CenterSync usersync = new User_CenterSync();
                usersync.SYNC();
            }




        }
    }
}
